<?php
/**
 * 充值订单提交接口
 */
namespace Api\Controller;

use Think\Controller;

class RechargeController extends Controller {

    public function _initialize() {
	    $this->m_recharge = D("Admin/Recharge");
	    $this->m_card = D("Admin/Card");
	    $this->m_logistics = D("Admin/Logistics");
    }

    public function index() {
    	exit(json_encode(array('x'=>'y')));
    }

    /*获取今日消费总数*/
    public function total() {
	    $sale_id = I("post.sale_id");
	    // $sale_id = 18;

	    $start_time = strtotime(date('Y-m-d 00:00:00'));
	    $end_time = strtotime(date('Y-m-d 23:59:59'));

	    $where = array(
	    	'lid'=>'1',	// 个人充值
	    	'model'=>'2', // 会员充值
	    	'sale_id'=>$sale_id,
	    	'create_time'=>array(
	    		"BETWEEN",
	    		array(
	    			$start_time,
	    			$end_time
	    		)
	    	)
	    );
        $total_price = $this->m_recharge->where($where)->sum('price');

    	exit(json_return('1','',array('total_price'=>number_format($total_price,2))));
    }

    public function detail(){
        $detail_id = I("request.detail_id");

        $recharge_data = $this->m_recharge->where(array("id"=>$detail_id))->find();
        $recharge_data['total_add_point'] = $recharge_data['general_point'] + $recharge_data['self_point'];

        $card_data = $this->m_card->where(array('id'=>$recharge_data['rid']))->find();

        // $logistics = M("Logistics")->where(array("id"=>$recharge_data['lid']))->find();

        $sale = M("Sale")->where(array("id"=>$recharge_data['sale_id']))->find();

        $logistics_user = M('users')->where(array('id'=>$recharge_data['uid']))->find();

        $data = array(
            'title' => '充值记录单',
            'detail_id' => $detail_id,
            'content' =>array(
                array(
                    'name' => '流水号',
                    'value' => $recharge_data['number'],
                ),
                array(
                    'name' => '充值时间',
                    'value' => date("Y-m-d H:i:s",$recharge_data['create_time']),
                ),
                array(
                    'name' => '账单名目',
                    'value' => '卡片充值',
                ),
                array(
                    'name' => '充值金额',
                    'value' => $recharge_data['price'],
                ),
                array(
                    'name' => '充值卡号',
                    'value' => $card_data['number'],
                ),
                array(
                    'name' => '卡内余额',
                    'value' => $card_data['balance'].'',
                ),
                array(
                    'name' => '卡内积分',
                    'value' => $card_data['point'].'',
                ),
                array(
                    'name' => '充值机构',
                    'value' => $sale['sale_name'].'',
                ),
                array(
                    'name' => '操作员',
                    'value' => $logistics_user['user_nicename'],
                ),
            ),
        );
        exit(json_return('1','',$data));
    }

    public function rechargepost(){
    	$sale_id = I("post.sale_id");
    	$user_id = I("post.user_id");
    	$card_number = I("post.card_number");
    	$price = I("post.recharge_price");
    	$desc = I("post.desc");

    	// $lid = 1;	// 个人账户的物流公司

    	$card_info = $this->m_card->where(array('number'=>$card_number))->find();
    	if ($card_info['lid'] != $lid) {
    		// 更换物流公司为个人
            // $this->m_card->where(array('number'=>$card_number))->save(array('lid'=>$lid));
            
            $this->m_logistics->where(array('id'=>$lid))->setInc('total_card_num');
            if ($card_info['is_active']) {
	            $this->m_logistics->where(array('id'=>$lid))->setInc('active_card_num');
            }
    	}

    	if(empty($card_number)){
            exit(json_return('0','卡号不能为空！'));
    	}
    	if(empty($price)){
            exit(json_return('0','充值金额不能为空！'));
    	}
    	$result =  $this->m_card->pre_recharge_price($lid, $card_number,$price,$desc,$sale_id,$user_id);
    	if($result === -2){
            exit(json_return('0','充值失败，金额不足！'));
    	}
    	if($result === -3){
            exit(json_return('0','充值失败，充值金额不足以开卡！'));
    	}
    	if ($result) {
            exit(json_return('1','',$result));
    	    // $this->success("充值成功！");
    	} else {
            exit(json_return('0','充值失败！'));
    	}
    }

    public function rechargeupdate(){
        $recharge_number = I("post.recharge_number");
        $result =  $this->m_recharge->update_status($recharge_number);
        if ($result) {
            push_recharge($result['id']);
            exit(json_return('1','',array('detail_id'=>$result['id'])));
            // $this->success("充值成功！");
        } else {
            exit(json_return('0','回调失败！'));
        }
    }
}

